

********************************************************************************************
*  run regressions and create output tables for latex 
********************************************************************************************
 
local file_path_in "/tochange/"
local file_path_code "/tochange/" 

timer clear 1
timer on 1

clear 
clear mata
clear matrix

set maxvar 32000
set matsize 11000
set memory 300g
set more off 

local regtype `1'  
local reghetero `2'
local regtimefe `3'
local regplacebo `4'
local regoutcome `5'
local fin_exploration `6'
local regsizeplot `7'
local outcomeyr `8'
local regcontrols `9'
local hpcontrol `10'  
local reglevel `11'    /* will be either "zip" or "fips"  */
local clustervar `12'
local inclpublic `13'
local regzipquality `14'
local houscharhetero `15'
local startyear `16'

local outcome `regoutcome'

if "`hpcontrol'"=="" {
	local hpZIL_control "loghpZIL_`reglevel'"  
	local hpZILdrop "loghpZIL_`reglevel'"
	local hpZIL_out "Yes"
	if "`reglevel'"=="fips" {
		local hpcontrol_out "County HP"
	}
	else local hpcontrol_out "Zip HP"
}
else if "`hpcontrol'"=="loghpZIL0609chg" {  
	local hpZIL_control "delta0609ZIL_`reglevel'"
	local hpZILdrop "delta0609ZIL_`reglevel'"
	local hpZIL_out "Yes"
	if "`reglevel'"=="fips" {
		local hpcontrol_out "Chg County HP"  
	}
	else local hpcontrol_out "Chg Zip HP"  
}

if "`regtimefe'"=="datefe" {
	local timefe "i.date"
	local timefeout "Yr by Mo."
}
else if "`regtimefe'"=="yearfe" {
	local timefe "i.year"
	local timefeout "Year"
}

local shock "shockdelta1yrZIL"
local finout "No"
local vardrop ""
local assetdrop ""
local finlabel ""
local fin ""
local samplecut ""
local publicsample ""
local outcome_controls ""
local outcome_term "No"
local outcome_ltv "No"
local outcome_mortgage_type "No"
local sqfeet "sqfeet"
local baths "baths"
local prop_code "i.PROPERTY_INDICATOR_CODE"
local sqfeet_drop "sqfeet"
local baths_drop "baths"
local prop_drop "*PROPERTY_INDICATOR_CODE*"
local spacefe "i.zip"
local spacefeout "Zip"

** update outcome vars
if  "`regoutcome'"=="hbloan" {
	local outcome_controls "mortgage_term i.mortgage_type"
	local samplecut  "financier==1 & " 
	local vardrop "mortgage_term *mortgage_type*"
	local outcome_term "Yes"
	local outcome_ltv "No"
	local outcome_mortgage_type "Yes"
}
local qpairfeout "No"
local return_sample ""

else if  "`regoutcome'"=="annual_return3"  {
	local qpair_fe "i.firstqdate##i.thirdqdate"  
	local qpairfeout "Yes"
	local return_sample " time3>2 & thirdqdate!=. & "
}
** county hp's
else if "`reghetero'"=="loghpZIL" {
	local shock "`reghetero'" 
	local hpZIL_control ""
	local hpZILdrop ""
	local hpZIL_out "No"
}
** sample construction 
if "`regtype'"!="chg0609" & "`regoutcome'"!="time_mkt" {
	
	use "`file_path_in'/HOUSEBUILDERshockHHI.dta", clear
	keep if error==0
	keep if small==0
	drop if nfipsyr<=2
	drop if land_sale==1
	
	** calculate number of homes sold per builder
	bysort seller year: gen nhomes = _n
	bysort seller year: replace nhomes=nhomes[_N]
	
	** calculate revenue per builder per year 
	bysort seller year: gen rev_all = sum(saleamount)
	bysort seller year: replace rev_all = rev_all[_N]
	
	** calculate revenue per builder in 2002
	g rev_all_02 = rev_all if year==2002
	bysort seller: egen temp = max(rev_all_02)
	drop rev_all_02 
	rename temp rev_all_02
	g lnrev_all02 = ln(rev_all_02)
	
	** calculate revenue per builder in 2004
	g rev_all_04 = rev_all if year==2004
	bysort seller: egen temp = max(rev_all_04)
	drop rev_all_04 
	rename temp rev_all_04
	
	** calculate revenue per builder in 2005
	g rev_all_05 = rev_all if year==2005
	bysort seller: egen temp = max(rev_all_05)
	drop rev_all_05 
	rename temp rev_all_05
	
	** calculate revenue per builder in 2006
	g rev_all_06 = rev_all if year==2006
	bysort seller: egen temp = max(rev_all_06)
	drop rev_all_06 
	rename temp rev_all_06
	g lnrev_all06 = ln(rev_all_06)
	
	** calculate revenue per builder in 2009
	g rev_all_09 = rev_all if year==2009
	bysort seller: egen temp = max(rev_all_09)
	drop rev_all_09 
	rename temp rev_all_09
	g lnrev_all09 = ln(rev_all_09)
	
	** calculate number of homes sold per builder in 2001
	g nhomes_01 = nhomes if year==2001
	bysort seller: egen temp = max(nhomes_01)
	drop nhomes_01 
	rename temp nhomes_01
	
	** calculate number of homes sold per builder in 2002
	g nhomes_02 = nhomes if year==2002
	bysort seller: egen temp = max(nhomes_02)
	drop nhomes_02 
	rename temp nhomes_02
	g lnhomes02 = ln(nhomes_02)
	
	** calculate number of homes sold per builder in 2003
	g nhomes_03 = nhomes if year==2003
	bysort seller: egen temp = max(nhomes_03)
	drop nhomes_03 
	rename temp nhomes_03
	
	** calculate number of homes sold per builder in 2004
	g nhomes_04 = nhomes if year==2004
	bysort seller: egen temp = max(nhomes_04)
	drop nhomes_04 
	rename temp nhomes_04
	
	** calculate number of homes sold per builder in 2005
	g nhomes_05 = nhomes if year==2005
	bysort seller: egen temp = max(nhomes_05)
	drop nhomes_05 
	rename temp nhomes_05
	
	** calculate number of homes sold per builder in 2006
	g nhomes_06 = nhomes if year==2006
	bysort seller: egen temp = max(nhomes_06)
	drop nhomes_06 
	rename temp nhomes_06
	g lnhomes06 = ln(nhomes_06)
	
	** calculate number of homes sold per builder in 2008
	g nhomes_08 = nhomes if year==2008
	bysort seller: egen temp = max(nhomes_08)
	drop nhomes_08 
	rename temp nhomes_08
	
	** calculate number of homes sold per builder in 2009
	g nhomes_09 = nhomes if year==2009
	bysort seller: egen temp = max(nhomes_09)
	drop nhomes_09 
	rename temp nhomes_09
	
	** calculate number of homes sold per builder in 2011
	g nhomes_11 = nhomes if year==2011
	bysort seller: egen temp = max(nhomes_11)
	drop nhomes_11 
	rename temp nhomes_11
		
	** calculate number of states per builder
	bysort seller year state: gen n_state= _n==1
	bysort seller year: gen nstate = sum(n_state)
	bysort seller year: replace nstate=nstate[_N]

	*** Inventory to sales ratio, 2006
	g inventory_06 = 1 if yearbuilt<2006 &  yearbuilt!=0  & (date>=tm(2006m7) & date<=tm(2006m12))
	g sales_06 = 1 if year==2006

	sort seller year yearbuilt
	bysort seller : g inv_06_sum = sum(inventory_06) 
	bysort seller : egen inv_06_max = max(inv_06_sum)
	
	bysort seller : g sales_06_sum = sum(sales_06)  
	bysort seller : egen sales_06_max = max(sales_06_sum)
	
	bysort seller : g inv_sales_06 = inv_06_max / sales_06_max 

	drop sales_06 inv_06_sum* sales_06_sum* 
	
	** create listing time
	gen listing = (year - yearbuilt)  
	label var listing "ListingTime"
	
	** housing attributes, clean up
	encode ROOF_TYPE_CODE, gen(roof)
	encode CONDITION_CODE, gen(condition)
	encode FIREPLACE_INDICATOR_FLAG, gen(fireplace)
	g fire = FIREPLACE_N>0 & !mi(FIREPLACE_N)
	replace BEDROOMS = . if BEDROOMS==0
	
	label var sqfeet "Sq. Ft."
	label var fire "Fireplace"
	label var BEDROOMS "Beds"	
			
	gen sf = PROPERTY_INDICATOR_CODE==10
	gen condo = PROPERTY_INDICATOR_CODE==11
		
	** adjust shock variable
	if "`regtype'"=="main09" {
		drop shockdelta1yrZIL_`reglevel'
		rename shockdelta0609ZIL_`reglevel' shockdelta1yrZIL
	}
	** out of state counties
	else if "`regtype'"=="outstate09"  {
		drop shockdelta1yrZIL_fips
		rename shockdelta0609ZIL_noadj_fips shockdelta1yrZIL
	}
	** boom period shock
	local boomtime `=substr("`regtype'",5,8)'
	di "`boomtime'"
	else if "`regtype'"=="boom`boomtime'" {
		drop shockdelta1yrZIL_`reglevel'
		rename shockdelta`boomtime'ZIL_`reglevel' shockdelta1yrZIL
	}		
	
	drop if shockdelta1yrZIL==0 
	
	** pull in finances 
	merge m:1 seller year quarter using "`file_path_in'/financialsq.dta", keep(master match)
	drop _merge
	gen lnassets_a=log(assets_a)
	gen lnassets_q=log(assets_q)
	
	label var shockdelta1yrZIL "shock"
	replace sqfeet = sqfeet*1000 
	
	** lnassets
	g assets_a09 = assets_a
	g lnassets_a09 = ln(assets_a09)
	
	** zipcode
	drop zipcode
	encode(zip), gen(todrop1)
	rename zip zipcode
	rename todrop1 zip
	destring zipcode, force replace

	** sale price
	gen lnsaleamt = log(saleamount)
	label var lnsaleamt "Log Sale Price"

	** make seller num to be clustervar
	egen long builder_no = group(seller)

	rename FIPS_CODE fips

	save "`file_path_in'/forplaceboreg.dta", replace 
	
	if "`regplacebo'"=="" & "`outcomeyr'"=="07" {
		keep if year==2007 
	}
	else if "`regplacebo'"==""  & "`outcomeyr'"=="08" {
		keep if year==2008 
	}
	else if "`regplacebo'"==""  & "`outcomeyr'"=="09" {
		keep if year==2009 
		bysort seller: egen financier = max(hbloan) 
	}
	else if  "`regplacebo'"==""  & "`outcomeyr'"=="crisisyrs"  {
		keep if year==2009 | year==2010 | year==2011 | year==2012 
	}
	if "`regoutcome'"=="listing" | "`regoutcome'"=="notsoldbuilt" | "`regoutcome'"=="time_mktGENERATE" {
		keep if year>=yearbuilt
		gen notsoldbuilt = yearbuilt != year  /* year sold is not same as yearbuilt */
		label var notsoldbuilt "Not Sold in Year Built"
	}
	** check non overlapping 
	else if "`regtype'"=="main09" & "`regplacebo'"==""  & "`outcomeyr'"=="10"  {
		keep if year==2010 
	}
	else if "`regtype'"=="boom`boomtime'"   & "`regplacebo'"=="" {
		local end `=substr("`boomtime'",3,4)'
		local end "`outcomeyr'"
		keep if year==20`end'
	}
	}
	
	local clusters `clustervar' 
	
	** puts out main dataset, for main specification, to merge with MLS
	if "`regtype'"=="main09" & "`regoutcome'"=="time_mktGENERATE" &  "`outcomeyr'"=="09" & "`reglevel'"=="fips"   & "`regplacebo'"=="" & "`fin_exploration'"==""  & "`regsizeplot'"!="sizeplot" & "`regoutcome'"!="no_sales" & "`regoutcome'"!="duration" & "`regcontrols'"!="allcontrols" & "`durationplot'"!="duration" & "`regzipquality'" == "" & "`houscharhetero'" == "" {
	preserve
		keep apnold APN_UNFORMATTED APN_SEQUENCE_NUMBER PROPERTY_STREET_NAME PROPERTY_CITY PROPERTY_STATE PROPERTY_HOUSE_NUMBER ///
			lnsaleamt saleamount shockdelta1yrZIL mortgage_term mortgage_type ltv mortgage_rate loghpZIL_fips loghpZIL_zip delta0609ZIL_fips delta0609ZIL_zip lnassets_a  last_sale SUBDIVISION_NAME ///
			yearbuilt year date RECORDING_DATE coverage_q06 listing hbloan time2 time3 secondqdate firstqdate thirdqdate sqfeet baths PROPERTY_INDICATOR_CODE year zip builder_no   fips nstate nhomes_06  rev_all_06 rev_all_09 assets_a06 assets_a09 coverage_q06_mean
		saveold "`file_path_in'/main_specification2.dta", version(13) replace
	restore	
	} 
	
	***************************** MAIN TABLE 3 ***************************** 
	if "`regtype'"!="chg0609" & "`regplacebo'"=="" & "`fin_exploration'"=="" & "`regoutcome'"!="lnhpzilzip" & "`regoutcome'"!="time_mkt"  & "`regoutcome'"!="time_mktGENERATE"  &  "`regsizeplot'"!="sizeplot" & "`regoutcome'"!="no_sales" & "`regoutcome'"!="duration" & "`regcontrols'"!="allcontrols" & "`regcontrols'"!="shockattributes" & "`durationplot'"!="duration" & "`regzipquality'" == ""  & "`houscharhetero'" == "" {   
		foreach s of local clusters {
		 
			reghdfe `outcome' `shock' `outcome_controls' `hpZIL_control'   if    `samplecut' `return_sample'  !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			estadd local sqfeet "No", replace
			estadd local baths "No", replace
			estadd local fixedprop "No", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "`finout'", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c1

			reghdfe `outcome' `shock' `outcome_controls' `hpZIL_control'   `sqfeet'  if    `samplecut' `return_sample'    !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			if "`sqfeet'"=="sqfeet" {
				estadd local sqfeet "Yes", replace
			}
			else estadd local sqfeet "No", replace
			estadd local baths "No", replace
			estadd local fixedprop "No", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "`finout'", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c2

			  reghdfe `outcome' `shock' `outcome_controls' `hpZIL_control'    `sqfeet' `baths'  if     `samplecut' `return_sample'    !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			if "`sqfeet'"=="sqfeet" {
				estadd local sqfeet "Yes", replace
			}
			else estadd local sqfeet "No", replace
			if "`baths'"=="baths" {
				estadd local baths "Yes", replace
			}
			else estadd local baths "No", replace
			estadd local fixedprop "No", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "`finout'", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c3

			  reghdfe `outcome' `shock' `outcome_controls' `hpZIL_control'    `sqfeet' `baths' `prop_code'  if     `samplecut' `return_sample'    !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			if "`sqfeet'"=="sqfeet" {
				estadd local sqfeet "Yes", replace
			}
			else estadd local sqfeet "No", replace
			if "`baths'"=="baths" {
				estadd local baths "Yes", replace
			}
			else estadd local baths "No", replace
			if "`prop_code'"=="i.PROPERTY_INDICATOR_CODE" {
				estadd local fixedprop "Yes", replace
			}
			else estadd local fixedprop "No", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "`finout'", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c4

			if "`inclpublic'"=="inclpublic" {
				
				** reg prices on terciles of shock 
				egen shocktile333 = xtile(shockdelta1yrZIL) if !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , nq(3)
				reghdfe lnsaleamt i.shocktile333 loghpZIL_f sqfeet baths i.PROPERTY_IND   if !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_IND) & !mi(shockdelta1yrZIL) , absorb(i.year##i.zip) vce(cluster fips)
				estadd local fixedyr "Yes" , replace
				estadd local fixedfips "Yes" , replace
				estadd local fixedyrfips "Yes" , replace
				estadd local hpZIL "`hpZIL_out'", replace
				estadd local sqfeet "Yes", replace
				estadd local baths "Yes", replace
				estadd local fixedprop "Yes", replace
				estadd local qpairfe "`qpairfeout'", replace
				estadd local lnassets "No", replace
				estadd local mort_term "`outcome_term'", replace
				estadd local mort_ltv "`outcome_ltv'", replace
				estadd local mort_type "`outcome_mortgage_type'", replace
				est sto c8terc
				
				**  public only sample , control for housing char 1
				reghdfe   lnsaleamt shockdelta1yrZIL loghpZIL_f lnassets_a if !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_IND) & !mi(shockdelta1yrZIL) & !mi(coverage_q06_mean) , absorb(i.year##i.zip) vce(cluster fips)
				estadd local fixedyr "Yes" , replace
				estadd local fixedfips "Yes" , replace
				estadd local fixedyrfips "Yes" , replace
				estadd local hpZIL "`hpZIL_out'", replace
				estadd local sqfeet "No", replace
				estadd local baths "No", replace
				estadd local fixedprop "No", replace
				estadd local qpairfe "`qpairfeout'", replace
				estadd local lnassets "No", replace
				estadd local mort_term "`outcome_term'", replace
				estadd local mort_ltv "`outcome_ltv'", replace
				estadd local mort_type "`outcome_mortgage_type'", replace
				est sto c9puba
				
				**  public only sample , control for housing char 2
				reghdfe   lnsaleamt shockdelta1yrZIL loghpZIL_f sqfeet baths i.PROPERTY_IND lnassets_a  if !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE)  & !mi(coverage_q06_mean), absorb(i.year##i.zip) vce(cluster fips)
				estadd local fixedyr "Yes" , replace
				estadd local fixedfips "Yes" , replace
				estadd local fixedyrfips "Yes" , replace
				estadd local hpZIL "`hpZIL_out'", replace
				estadd local sqfeet "Yes", replace
				estadd local baths "Yes", replace
				estadd local fixedprop "Yes", replace
				estadd local qpairfe "`qpairfeout'", replace
				estadd local lnassets "No", replace
				estadd local mort_term "`outcome_term'", replace
				estadd local mort_ltv "`outcome_ltv'", replace
				estadd local mort_type "`outcome_mortgage_type'", replace
				est sto c9pubb
				
				**  public only sample 
				reghdfe   lnsaleamt shockdelta1yrZIL loghpZIL_f assets_a06 , absorb(i.year##i.zip) vce(cluster fips)
				estadd local fixedyr "Yes" , replace
				estadd local fixedfips "Yes" , replace
				estadd local fixedyrfips "Yes" , replace
				estadd local hpZIL "`hpZIL_out'", replace
				estadd local sqfeet "No", replace
				estadd local baths "No", replace
				estadd local fixedprop "No", replace
				estadd local qpairfe "`qpairfeout'", replace
				estadd local lnassets "Yes", replace
				estadd local mort_term "`outcome_term'", replace
				estadd local mort_ltv "`outcome_ltv'", replace
				estadd local mort_type "`outcome_mortgage_type'", replace
				est sto c10pub
				
				* in public sample, control for size (revenues)
				local size_control ""
				if "`regtype'"=="main09" {
					local size_control "rev_all_06"
				}
				else if "`regtype'"=="boom0205" {
					local size_control "rev_all_02"
				}
				reghdfe `outcome' `shock' `outcome_controls' `hpZIL_control'   `size_control'  if     `samplecut' `return_sample'    !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
				estadd local fixedyr "Yes" , replace
				estadd local fixedfips "Yes" , replace
				estadd local fixedyrfips "Yes" , replace
				estadd local hpZIL "`hpZIL_out'", replace
				if "`sqfeet'"=="sqfeet" {
					estadd local sqfeet "Yes", replace
				}
				else estadd local sqfeet "No", replace
				if "`baths'"=="baths" {
					estadd local baths "Yes", replace
				}
				else estadd local baths "No", replace
				if "`prop_code'"=="i.PROPERTY_INDICATOR_CODE" {
					estadd local fixedprop "Yes", replace
				}
				else estadd local fixedprop "No", replace
				estadd local qpairfe "`qpairfeout'", replace
				estadd local lnassets "Yes", replace
				estadd local mort_term "`outcome_term'", replace
				estadd local mort_ltv "`outcome_ltv'", replace
				estadd local mort_type "`outcome_mortgage_type'", replace
				est sto c9
				
				** main table (TABLE 3)
				esttab  c1 c2 c3 c4 c8terc c10pub using ///   
					"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'MAIN_TABLE.csv",   replace se label ///
					s(fixedyr fixedfips fixedyrfips hpZIL sqfeet baths fixedprop  qpairfe  mort_term mort_ltv mort_type lnassets r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "`hpcontrol_out'" ///
					"Sq. Ft." "Baths" "Prop. Type FE"  "Builder FE" "2nd Sale Pair FE" "Mort. Term" "LTV" "Mort. Type" "Ln Assets" "\$ R^2 \$")) substitute(\_ _) mgroups("All Firms" "Public Only", pattern(1 0 0 0 0 1 ))  ///
						mtitles("Price" "Price" "Price" "Price" "Price" "Price")  ///
						drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop' `vardrop' assets_a06 )  varwidth(1) `finlabel' nonotes ///
						coeflabels(2.shocktile333 "mid shock tercile" 3.shocktile333 "top shock tercile" shockdelta1yrZIL "shock") /// 
						starlevels(* 0.10 ** 0.05 *** 0.01) 
						
				esttab  c1 c2 c3 c4 c8terc c10pub using ///   
					"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'MAIN_TABLE.tex",   replace se label ///
					s(fixedyr fixedfips fixedyrfips hpZIL sqfeet baths fixedprop  qpairfe  mort_term mort_ltv mort_type lnassets r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "`hpcontrol_out'" ///
					"Sq. Ft." "Baths" "Prop. Type FE"  "Builder FE" "2nd Sale Pair FE" "Mort. Term" "LTV" "Mort. Type" "Ln Assets" "\$ R^2 \$")) substitute(\_ _) mgroups("All Firms" "Public Only", pattern(1 0 0 0 0 1 ))  ///
						mtitles("Price" "Price" "Price" "Price" "Price" "Price")  ///
						drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop' `vardrop' assets_a06 )  varwidth(1) `finlabel' nonotes ///
						coeflabels(2.shocktile333 "mid shock tercile" 3.shocktile333 "top shock tercile" shockdelta1yrZIL "shock") ///
						starlevels(* 0.10 ** 0.05 *** 0.01) 
			
			** BOOM ROBUSTNESS: TABLE 19
			if "`regtype'"=="boom0205"  { 
				esttab  c1 c2 c3 c4 c9puba c9pubb using ///   
					"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'TABLE19.csv",   replace se label ///
					s(fixedyr fixedfips fixedyrfips hpZIL sqfeet baths fixedprop  r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "`hpcontrol_out'" ///
					"Sq. Ft." "Baths" "Prop. Type FE"    "\$ R^2 \$")) substitute(\_ _) mgroups("All Firms" "Public Only", pattern(1 0 0 0 0 1 ))  ///
						mtitles("Price" "Price" "Price" "Price" "Price" "Price")  ///
						drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop' `vardrop' lnassets_a)  varwidth(1) `finlabel' nonotes ///
						coeflabels( shockdelta1yrZIL "shock") ///
						starlevels(* 0.10 ** 0.05 *** 0.01) 
				
				esttab  c1 c2 c3 c4 c9puba c9pubb using ///   
					"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'TABLE19.tex",   replace se label ///
					s(fixedyr fixedfips fixedyrfips hpZIL sqfeet baths fixedprop  r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "`hpcontrol_out'" ///
					"Sq. Ft." "Baths" "Prop. Type FE"    "\$ R^2 \$")) substitute(\_ _) mgroups("All Firms" "Public Only", pattern(1 0 0 0 0 1 ))  ///
						mtitles("Price" "Price" "Price" "Price" "Price" "Price")  ///
						drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop' `vardrop' lnassets_a)  varwidth(1) `finlabel' nonotes ///
						coeflabels( shockdelta1yrZIL "shock") ///
						starlevels(* 0.10 ** 0.05 *** 0.01) 
				
				** control for size in regression using shock in 2005 - THIS IS COLUMN 8 RESULT IN TABLE 4
				esttab  c9 using ///   
					"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'_sizecontrol.csv",   replace se label ///
					s(fixedyr fixedfips fixedyrfips hpZIL sqfeet baths fixedprop  qpairfe  mort_term mort_ltv mort_type lnassets r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "`hpcontrol_out'" ///
					"Sq. Ft." "Baths" "Prop. Type FE"  "Builder FE" "2nd Sale Pair FE" "Mort. Term" "LTV" "Mort. Type" "Ln Sales" "\$ R^2 \$")) substitute(\_ _) mgroups("All Firms" , pattern(1 ))  ///
						mtitles("Price" )  ///
						drop(`hpZILdrop' `size_control' )  varwidth(1) `finlabel' nonotes ///						
						starlevels(* 0.10 ** 0.05 *** 0.01)		
			}
			
			}
			else {
				** main table 
				esttab  c1 c2 c3 c4  using ///   
					"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'MAIN_TABLE.csv",   replace se label ///
					s(fixedyr fixedfips fixedyrfips hpZIL sqfeet baths fixedprop  qpairfe  mort_term mort_ltv mort_type lnassets r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "`hpcontrol_out'" ///
					"Sq. Ft." "Baths" "Prop. Type FE"  "Builder FE" "2nd Sale Pair FE" "Mort. Term" "LTV" "Mort. Type" "Ln Assets" "\$ R^2 \$")) substitute(\_ _) mgroups("All Firms" , pattern(1 0 0 0  ))  ///
						mtitles("Price" "Price" "Price" "Price")  ///
						drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop' `vardrop' )  varwidth(1) `finlabel' nonotes ///
						starlevels(* 0.10 ** 0.05 *** 0.01) 
						
				esttab  c1 c2 c3 c4  using ///   
					"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'MAIN_TABLE.tex",   replace se label ///
					s(fixedyr fixedfips fixedyrfips hpZIL sqfeet baths fixedprop  qpairfe  mort_term mort_ltv mort_type lnassets r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "`hpcontrol_out'" ///
					"Sq. Ft." "Baths" "Prop. Type FE"  "Builder FE" "2nd Sale Pair FE" "Mort. Term" "LTV" "Mort. Type" "Ln Assets" "\$ R^2 \$")) substitute(\_ _) mgroups("All Firms" , pattern(1 0 0 0  ))  ///
						mtitles("Price" "Price" "Price" "Price")  ///
						drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop' `vardrop' )  varwidth(1) `finlabel' nonotes ///
						starlevels(* 0.10 ** 0.05 *** 0.01) 		
			}
			}
			}
			
			************************* annualized return on home ************************* 
			if "`regoutcome'"=="annual_return3"  & "`regtype'"!="chg0609" & "`regplacebo'"=="" & "`fin_exploration'"=="" & "`regoutcome'"!="lnhpzilzip" & "`regoutcome'"!="time_mkt"  & "`regoutcome'"!="time_mktGENERATE"  &  "`regsizeplot'"!="sizeplot" & "`regoutcome'"!="no_sales" & "`regoutcome'"!="duration" & "`regcontrols'"!="allcontrols" & "`regcontrols'"!="shockattributes" & "`durationplot'"!="duration" & "`regzipquality'" == ""  & "`houscharhetero'" == "" {   
			foreach s of local clusters {	
				**  table on heterogeneity of shock over time on returns
				g year3=round(time3/4,1)
				*reghdfe annual_return3 c.time3##c.shockdelta1yrZIL loghpZIL_f sqfeet baths i.PROPERTY_IND  if  time3>2 & thirdqdate!=. & !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_IN) & !mi(shockdelta1yrZIL) , absorb(i.year##i.zip i.firstqdate##i.thirdqdate) vce(cluster fips)
				reghdfe `outcome' c.time3##c.`shock' `outcome_controls' `hpZIL_control'    `sqfeet' `baths' `prop_code'  if     `samplecut' `return_sample'    !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
				estadd local fixedyr "Yes" , replace
				estadd local fixedfips "Yes" , replace
				estadd local fixedyrfips "Yes" , replace
				estadd local hpZIL "`hpZIL_out'", replace
				if "`sqfeet'"=="sqfeet" {
					estadd local sqfeet "Yes", replace
				}
				else estadd local sqfeet "No", replace
				if "`baths'"=="baths" {
					estadd local baths "Yes", replace
				}
				else estadd local baths "No", replace
				if "`prop_code'"=="i.PROPERTY_INDICATOR_CODE" {
					estadd local fixedprop "Yes", replace
				}
				else estadd local fixedprop "No", replace
				estadd local qpairfe "`qpairfeout'", replace
				estadd local lnassets "`finout'", replace
				estadd local mort_term "`outcome_term'", replace
				estadd local mort_ltv "`outcome_ltv'", replace
				estadd local mort_type "`outcome_mortgage_type'", replace
				est sto cret1
				
				* reghdfe `outcome' i.year3##c.`shock' `outcome_controls' `hpZIL_control'    `sqfeet' `baths' `prop_code'  if     `samplecut' `return_sample'    !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
				reghdfe annual_return3 i.year3##c.shockdelta1yrZIL loghpZIL_f sqfeet baths i.PROPERTY_IND  if  time3>2 & thirdqdate!=. & !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_IN) & !mi(shockdelta1yrZIL) , absorb(i.year##i.zip i.firstqdate##i.thirdqdate) vce(cluster fips)
				
				estadd local fixedyr "Yes" , replace
				estadd local fixedfips "Yes" , replace
				estadd local fixedyrfips "Yes" , replace
				estadd local hpZIL "`hpZIL_out'", replace
				if "`sqfeet'"=="sqfeet" {
					estadd local sqfeet "Yes", replace
				}
				else estadd local sqfeet "No", replace
				if "`baths'"=="baths" {
					estadd local baths "Yes", replace
				}
				else estadd local baths "No", replace
				if "`prop_code'"=="i.PROPERTY_INDICATOR_CODE" {
					estadd local fixedprop "Yes", replace
				}
				else estadd local fixedprop "No", replace
				estadd local qpairfe "`qpairfeout'", replace
				estadd local lnassets "`finout'", replace
				estadd local mort_term "`outcome_term'", replace
				estadd local mort_ltv "`outcome_ltv'", replace
				estadd local mort_type "`outcome_mortgage_type'", replace
				est sto cret2
				
				** for excel output 
				tab year3  if !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_IND) & time3>2 & thirdqdate!=. & !mi(shockdelta1yrZIL)
				
				preserve
					g nhomes_annual = 1
					collapse (sum) nhomes_annual if !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_IND) & time3>2 & thirdqdate!=. & !mi(shockdelta1yrZIL) , by( year3)
					outsheet using "`file_path_in'/reg output/annual_return_obs_year.csv", comma replace 
				restore 
				
				esttab  c1 c2 c3 c4 cret1 cret2  using ///   
					"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'_return.csv",   replace se label ///
					s(fixedyr fixedfips fixedyrfips hpZIL sqfeet baths fixedprop  qpairfe  mort_term mort_ltv mort_type lnassets r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "`hpcontrol_out'" ///
					"Sq. Ft." "Baths" "Prop. Type FE"  "Builder FE" "2nd Sale Pair FE" "Mort. Term" "LTV" "Mort. Type" "Ln Assets" "\$ R^2 \$")) substitute(\_ _)  mgroups("All Firms", pattern(1 0 0 0 0 0 ))  ///
						mtitles("Return" "Return" "Return" "Return" "Return" "Return" ) coeflabels(c.time3#c.shockdelta1yrZIL "Shock X Time Between Sales"  1.year3#c.shockdelta1yrZIL "Shock X Year 1"  ///
						2.year3#c.shockdelta1yrZIL "Shock X Year 2" 3.year3#c.shockdelta1yrZIL "Shock X Year 3" 4.year3#c.shockdelta1yrZIL "Shock X Year 4" 5.year3#c.shockdelta1yrZIL "Shock X Year 5" 6.year3#c.shockdelta1yrZIL "Shock X Year 6")  ///
						drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop' `vardrop'  2.year3 3.year3  4.year3 5.year3 6.year3 time3 )  varwidth(1) `finlabel' nonotes   starlevels(* 0.10 ** 0.05 *** 0.01)
						
				esttab  c1 c2 c3 c4 cret1 cret2  using ///   
					"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'_return.tex",   replace se label ///
					s(fixedyr fixedfips fixedyrfips hpZIL sqfeet baths fixedprop  qpairfe  mort_term mort_ltv mort_type lnassets r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "`hpcontrol_out'" ///
					"Sq. Ft." "Baths" "Prop. Type FE"  "Builder FE" "2nd Sale Pair FE" "Mort. Term" "LTV" "Mort. Type" "Ln Assets" "\$ R^2 \$")) substitute(\_ _)  mgroups("All Firms", pattern(1 0 0 0 0 0 ))  ///
						mtitles("Return" "Return" "Return" "Return" "Return" "Return" ) coeflabels(c.time3#c.shockdelta1yrZIL "Shock X Time Between Sales"  1.year3#c.shockdelta1yrZIL "Shock X Year 1"  ///
						2.year3#c.shockdelta1yrZIL "Shock X Year 2" 3.year3#c.shockdelta1yrZIL "Shock X Year 3" 4.year3#c.shockdelta1yrZIL "Shock X Year 4" 5.year3#c.shockdelta1yrZIL "Shock X Year 5" 6.year3#c.shockdelta1yrZIL "Shock X Year 6")  ///
						drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop' `vardrop'  2.year3 3.year3  4.year3 5.year3 6.year3 time3 )  varwidth(1) `finlabel' nonotes   starlevels(* 0.10 ** 0.05 *** 0.01)
						
			do "`file_path_code'/persistance-resales.do" 
		
			 }
			}
			
			********************* split sample by shock size ********************* 
			if "`reghetero'"=="asym" & "`regoutcome'"=="lnsaleamt"  & "`regtype'"!="chg0609" & "`regplacebo'"=="" & "`fin_exploration'"=="" & "`regoutcome'"!="lnhpzilzip" & "`regoutcome'"!="time_mkt"  & "`regoutcome'"!="time_mktGENERATE"  &  "`regsizeplot'"!="sizeplot" & "`regoutcome'"!="no_sales" & "`regoutcome'"!="duration" & "`regcontrols'"!="allcontrols" & "`regcontrols'"!="shockattributes" & "`durationplot'"!="duration" & "`regzipquality'" == ""  & "`houscharhetero'" == "" {   
			foreach s of local clusters {
			** negative shock results
			qui  reghdfe `outcome' `shock' `outcome_controls' `hpZIL_control'    `sqfeet' `baths' `prop_code'  if `shock'<0 &     `samplecut' `return_sample'    !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			if "`sqfeet'"=="sqfeet" {
				estadd local sqfeet "Yes", replace
			}
			else estadd local sqfeet "No", replace
			if "`baths'"=="baths" {
				estadd local baths "Yes", replace
			}
			else estadd local baths "No", replace
			if "`prop_code'"=="i.PROPERTY_INDICATOR_CODE" {
				estadd local fixedprop "Yes", replace
			}
			else estadd local fixedprop "No", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "`finout'", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c7size
			
			** positive shock results
			qui  reghdfe `outcome' `shock' `outcome_controls' `hpZIL_control'    `sqfeet' `baths' `prop_code'  if `shock'>=0 &     `samplecut' `return_sample'    !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			if "`sqfeet'"=="sqfeet" {
				estadd local sqfeet "Yes", replace
			}
			else estadd local sqfeet "No", replace
			if "`baths'"=="baths" {
				estadd local baths "Yes", replace
			}
			else estadd local baths "No", replace
			if "`prop_code'"=="i.PROPERTY_INDICATOR_CODE" {
				estadd local fixedprop "Yes", replace
			}
			else estadd local fixedprop "No", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "`finout'", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c8size
			
			** negative shock results - <-0.1
			qui  reghdfe `outcome' `shock' `outcome_controls' `hpZIL_control'    `sqfeet' `baths' `prop_code'  if `shock'<-.1 &     `samplecut' `return_sample'    !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			if "`sqfeet'"=="sqfeet" {
				estadd local sqfeet "Yes", replace
			}
			else estadd local sqfeet "No", replace
			if "`baths'"=="baths" {
				estadd local baths "Yes", replace
			}
			else estadd local baths "No", replace
			if "`prop_code'"=="i.PROPERTY_INDICATOR_CODE" {
				estadd local fixedprop "Yes", replace
			}
			else estadd local fixedprop "No", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "`finout'", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c9size
			
			** positive shock results - >-0.1
			qui  reghdfe `outcome' `shock' `outcome_controls' `hpZIL_control'    `sqfeet' `baths' `prop_code'  if `shock'>=-.1 &     `samplecut' `return_sample'    !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			if "`sqfeet'"=="sqfeet" {
				estadd local sqfeet "Yes", replace
			}
			else estadd local sqfeet "No", replace
			if "`baths'"=="baths" {
				estadd local baths "Yes", replace
			}
			else estadd local baths "No", replace
			if "`prop_code'"=="i.PROPERTY_INDICATOR_CODE" {
				estadd local fixedprop "Yes", replace
			}
			else estadd local fixedprop "No", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "`finout'", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c10size
			
			** slightly negative shock results - (-0.1,0)
			qui reghdfe lnsaleamt shockdelta1yrZIL sqfeet baths i.PROPERTY_IND loghpZIL_f     if shockdelta1yrZIL<=0 & shockdelta1yrZIL>=-.1   &      `samplecut' `return_sample'   !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) &  !mi(shockdelta1yrZIL), absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe' ) vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			if "`sqfeet'"=="sqfeet" {
				estadd local sqfeet "Yes", replace
			}
			else estadd local sqfeet "No", replace
			if "`baths'"=="baths" {
				estadd local baths "Yes", replace
			}
			else estadd local baths "No", replace
			if "`prop_code'"=="i.PROPERTY_INDICATOR_CODE" {
				estadd local fixedprop "Yes", replace
			}
			else estadd local fixedprop "No", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "`finout'", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c11size
			
			esttab  c7size c8size c9size c10size c11size using ///   
				"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'_pos_neg.csv",   replace se label ///
				s(fixedyr fixedfips fixedyrfips hpZIL sqfeet baths fixedprop  qpairfe  mort_term mort_ltv mort_type lnassets r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "`hpcontrol_out'" ///
				"Sq. Ft." "Baths" "Prop. Type FE"  "Builder FE" "2nd Sale Pair FE" "Mort. Term" "LTV" "Mort. Type" "Ln Assets" "\$ R^2 \$")) substitute(\_ _)  mgroups("Neg. Shock" "Pos. Shock" "Shock<-0.1" "Shock>-0.1" "Shock (-0.1,0]" , pattern(1  1  1  1  1  )) ///
					mtitles("Price" "Price" "Price" "Price" "Price" )  ///
					drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop' `vardrop' )  varwidth(1) `finlabel' nonotes   starlevels(* 0.10 ** 0.05 *** 0.01)
			esttab  c7size c8size c9size c10size c11size using ///   
				"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'_pos_neg.tex",   replace se label ///
				s(fixedyr fixedfips fixedyrfips hpZIL sqfeet baths fixedprop  qpairfe  mort_term mort_ltv mort_type lnassets r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "`hpcontrol_out'" ///
				"Sq. Ft." "Baths" "Prop. Type FE"  "Builder FE" "2nd Sale Pair FE" "Mort. Term" "LTV" "Mort. Type" "Ln Assets" "\$ R^2 \$")) substitute(\_ _) mgroups("Neg. Shock" "Pos. Shock" "Shock<-0.1" "Shock>-0.1" "Shock (-0.1,0]" , pattern(1  1  1  1  1  )) ///
					mtitles("Price" "Price" "Price" "Price" "Price" )  ///
					drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop' `vardrop' )  varwidth(1) `finlabel' nonotes   starlevels(* 0.10 ** 0.05 *** 0.01) 		
						
		}
		}
	
	******************************* ALL control variables *******************************
	local clusters fips 
	if "`regcontrols'"=="allcontrols" & "`regplacebo'"=="" & "`fin_exploration'"=="" & "`regoutcome'"!="lnhpzilzip" &  "`regsizeplot'"!="sizeplot" & "`regoutcome'"!="no_sales" & "`regoutcome'"!="duration"  & "`durationplot'"!="duration" &  "`regzipquality'" == ""  & "`houscharhetero'" == ""  {   
		foreach s of local clusters {
			
			qui  reghdfe `outcome' `shock' `outcome_controls' `hpZIL_control'  sqfeet baths i.PROPERTY_INDICATOR_CODE i.roof i.condition i.fire  if  ///
				   `samplecut' `return_sample'    !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) & !mi(roof) & !mi(condition) & !mi(fire), absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "Yes", replace
			estadd local sqfeet "Yes", replace
			estadd local baths "Yes", replace
			estadd local fixedprop "Yes", replace
			estadd local roof "Yes", replace
			estadd local condition "Yes", replace
			estadd local fire "Yes", replace
			est sto c25
				
				esttab  c25 using ///   
					"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'.csv",   replace se label ///
					s(fixedyr fixedfips fixedyrfips hpZIL sqfeet baths fixedprop roof fire condition lnassets r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "`hpcontrol_out'" ///
					"Sq. Ft." "Baths" "Prop. Type FE"  "Roof Type" "Fireplace" "Condition" "Ln Assets" "\$ R^2 \$")) substitute(\_ _)  ///
						mtitles("Price" )  ///
						drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop'  `vardrop' `assetdrop' *PROPERTY_INDICATOR_CODE* *roof* *fire* *condition* )  varwidth(1) `finlabel' nonotes ///
						starlevels(* 0.10 ** 0.05 *** 0.01) 
						
				esttab  c25 using ///   
					"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'.tex",   replace se label ///
					s(fixedyr fixedfips fixedyrfips hpZIL sqfeet baths fixedprop roof fire condition lnassets r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "`hpcontrol_out'" ///
					"Sq. Ft." "Baths" "Prop. Type FE"  "Roof Type" "Fireplace" "Condition" "Ln Assets" "\$ R^2 \$")) substitute(\_ _)  ///
						mtitles("Price" )  ///
						drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop'  `vardrop' `assetdrop' *PROPERTY_INDICATOR_CODE* *roof* *fire* *condition* )  varwidth(1) `finlabel' nonotes ///
						starlevels(* 0.10 ** 0.05 *** 0.01) 
		}
	}
	
	
	******************************* zipcode fe by quality fe *******************************
	local clusters fips 
	if "`regplacebo'"=="" & "`fin_exploration'"=="" & "`regoutcome'"!="lnhpzilzip" & "`regsizeplot'"!="sizeplot" & "`regoutcome'"!="no_sales" & "`regoutcome'"!="duration"  & "`regcontrols'"=="" & "`durationplot'"!="duration" &  "`regzipquality'"=="zipqu" {   
		foreach s of local clusters {
			
			qui  reghdfe `outcome' `shock' `outcome_controls' `hpZIL_control'   sqfeet baths i.PROPERTY_INDICATOR_CODE  if  ///
				   `samplecut' `return_sample'    !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' i.`spacefe'#c.sqfeet i.`spacefe'#c.baths i.`spacefe'#i.PROPERTY_INDICATOR_CODE `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "Yes", replace
			estadd local sqfeet "Yes", replace
			estadd local baths "Yes", replace
			estadd local fixedprop "Yes", replace
			estadd local fixedsqfeet "Yes", replace
			estadd local fixedbaths "Yes", replace
			estadd local fixedproptype "Yes", replace
			estadd local lnassets "No", replace
			est sto c4
			
			esttab   c4 using ///   
				"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'.csv",   replace se label ///
				s( fixedfips  hpZIL sqfeet baths fixedprop fixedsqfeet fixedbaths fixedproptype  r2 N , label("`spacefeout' FE"  "`hpcontrol_out'" ///
				"Sq. Ft." "Baths" "Prop. Type FE"  "`spacefeout' x Sq. Ft. FE" "`spacefeout'x Baths FE" "`spacefeout'x Prop. Type FE"   "\$ R^2 \$")) substitute(\_ _)   ///
					mtitles("Price" )  ///
					drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop'  `vardrop' `assetdrop'  *PROPERTY_INDICATOR_CODE*  )  varwidth(1) `finlabel' nonotes ///
						starlevels(* 0.10 ** 0.05 *** 0.01) 
						
			esttab   c4 using ///   
				"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'.tex",   replace se label ///
				s( fixedfips  hpZIL sqfeet baths fixedprop fixedsqfeet fixedbaths fixedproptype  r2 N , label("`spacefeout' FE"  "`hpcontrol_out'" ///
				"Sq. Ft." "Baths" "Prop. Type FE"  "`spacefeout' x Sq. Ft. FE" "`spacefeout'x Baths FE" "`spacefeout'x Prop. Type FE"   "\$ R^2 \$")) substitute(\_ _)   ///
					mtitles("Price" )  ///
					drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop'  `vardrop' `assetdrop'  *PROPERTY_INDICATOR_CODE*  )  varwidth(1) `finlabel' nonotes ///
						starlevels(* 0.10 ** 0.05 *** 0.01) 			
		}
	}
	
	******************************* parallel trends: interact shock with year dummies *******************************
	else if "`regplacebo'"=="placebo" & "`fin_exploration'"=="" & "`regoutcome'"!="lnhpzilzip" & "`regoutcome'"!="duration" & "`regsizeplot'"!="sizeplot" & "`regcontrols'"!="allcontrols" & "`durationplot'"!="duration" &  "`regzipquality'" == ""  & "`houscharhetero'" == ""    { 
		foreach s of local clusters {
				
				qui reg `outcome'  `shock' c.`shock'#ib2000.year ib2000.year `hpZIL_control' sqfeet baths i.PROPERTY_INDICATOR_CODE  `spacefe' if   !mi(sqfeet), vce(cluster `s')	
				estadd local fixedyr "Yes" , replace
				estadd local fixedfips "Yes" , replace
				estadd local fixedyrfips "Yes" , replace
				estadd local fixedprop "Yes", replace
				estadd local hpZIL "`hpZIL_out'", replace
				estadd local sqfeet "Yes", replace
				estadd local baths "Yes", replace
				estadd local qpairfe "`qpairfeout'", replace
				estadd local lnassets "`finout'", replace
				est sto c1
			}
			coefplot , keep(*year#c.shockdelta1yrZIL*) yline(0) vertical coeflabels(2001.year#c.shockdelta1yrZIL = "2001" 2002.year#c.shockdelta1yrZIL = "2002" 2003.year#c.shockdelta1yrZIL = "2003" 2004.year#c.shockdelta1yrZIL = "2004" ///
				2005.year#c.shockdelta1yrZIL = "2005" 2006.year#c.shockdelta1yrZIL = "2006" 2007.year#c.shockdelta1yrZIL = "2007" 2008.year#c.shockdelta1yrZIL = "2008" 2009.year#c.shockdelta1yrZIL = "2009" 2010.year#c.shockdelta1yrZIL = "2010" ///
				2011.year#c.shockdelta1yrZIL = "2011" 2012.year#c.shockdelta1yrZIL = "2012" 2013.year#c.shockdelta1yrZIL = "2013" 2014.year#c.shockdelta1yrZIL = "2014" 2015.year#c.shockdelta1yrZIL = "2015" )  ///
				ytitle("Coefficient on Builder Shock") xtitle("Year") graphregion(color(white))
			graph export "`file_path_in'/reg output//reg_placebo`regoutcome'`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'.png", replace
			graph export "`file_path_in'/reg output//reg_placebo`regoutcome'`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'.eps", replace		
	}
	
	********************************** fin heterogeneity analysis ********************************** 
	else if "`fin_exploration'"=="fin_explore" & "`regplacebo'"=="" & "`regoutcome'"!="lnhpzilzip" & "`regsizeplot'"!="sizeplot" & "`regoutcome'"!="duration" & "`regcontrols'"!="allcontrols" & "`durationplot'"!="duration" &  "`regzipquality'" == ""  & "`houscharhetero'" == ""   { 
	rename equity_unconsolsub_a eq_unconsol_a
	rename equity_unconsolsub_a06 eq_unconsol_a06
	
	local indep leverage_q06_mean coverage_q06_mean pctoption_a06 pcteq_subsassets_a06 eq_unconsol_a06 
	
	label var eq_unconsol_a06 "Eqty Subs._{06}"
	label var pcteq_subsassets_a06 "Eqty Subs. Assets_{06}"
	label var leverage_q06_mean "Leverage_{06}"
	label var coverage_q06_mean "Coverage_{06}"
	label var pctoption_a06 "Pct. Option_{06}"
	
	cap drop lnhomes06
	g lnhomes06 = log(nhomes_06)
	cap drop lnhomes09
	g lnhomes09 = log(nhomes_09)
	
		** summary stats for main hetero results, by distribution of builder ratios (consistent with Sum Stats Table 1)
		preserve
			g invt_revt_a06 = invent_total_a06 / revt_a06
			g undev_revt_a06 =  undev_invent_own_a06 / revt_a06
			collapse nhomes leverage_q06_mean coverage_q06_mean pcteq_subsassets_a06 eq_unconsol_a06 pctoption_a06 inv_sales_06 invt_revt_a06 undev_revt_a06 if !mi(shockdelta1yrZIL), by(seller year)
			eststo public: quietly estpost summarize nhomes  leverage_q06_mean coverage_q06_mean pcteq_subsassets_a06 eq_unconsol_a06 pctoption_a06 inv_sales_06 invt_revt_a06 undev_revt_a06 if coverage_q06_mean!=. , d
			esttab  public using "/`file_path_in'/reg output/TABLE4_summ_stats_twocolumns_pre.csv", cells("mean(pattern(1 1 0) fmt(4)) sd(pattern(1 1 0) fmt(4)) p25(pattern(1 1 0) fmt(4)) p50(pattern(1 1 0) fmt(4)) p75(pattern(1 1 0) fmt(4)) ") label nonum replace substitute(\_ _)
		restore	
		
		preserve
			collapse nhomes inv_sales_06 if !mi(shockdelta1yrZIL) & !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_IND), by(seller year)
			eststo all: quietly estpost summarize inv_sales_06 nhomes, d
		restore	
		esttab all public using "/`file_path_in'/reg output/TABLE4_summ_stats_twocolumns.csv", cells("mean(pattern(1 1 0) fmt(4)) sd(pattern(1 1 0) fmt(4)) p25(pattern(1 1 0) fmt(4)) p50(pattern(1 1 0) fmt(4)) p75(pattern(1 1 0) fmt(4)) ") label nonum replace substitute(\_ _)

		*******************************************************************************************************************
		* Table 4, main hetero 2006
		*******************************************************************************************************************
		label var rev_all_06 "Revenue_{06}"
		label var eq_unconsol_a06 "Eqty Subs._{06}"
		label var pcteq_subsassets_a06 "Eqty Subs. Assets_{06}"
		label var leverage_q06_mean "Leverage_{06}"
		label var coverage_q06_mean "Coverage_{06}"
		label var pctoption_a06 "Pct. Option_{06}"
		
		** rename fin constraints vars bc too long to save in estimation
		rename leverage_q06_mean lev
		rename coverage_q06_mean cov
		rename pctoption_a06 option
		rename pcteq_subsassets_a06 pcteq
		rename eq_unconsol_a06 equ
		
		local indep "lev cov option pcteq equ"
	
		foreach s of local clusters { 
			
				**** shock in public only sample  ***
				reghdfe `outcome' shockdelta1yrZIL revt_a06  `outcome_controls' `hpZIL_control'  if !mi(lev) &      `samplecut' `return_sample'    !mi(shockdelta1yrZIL), absorb(`timefe'#`spacefe' `timefe' `spacefe') vce(cluster `s')
					estadd local fixedyr "Yes" , replace
					estadd local fixedfips "Yes" , replace
					estadd local fixedyrfips "Yes" , replace
					estadd local sqfeet "No", replace
					estadd local hpZIL "`hpZIL_out'", replace
					estadd local assets "Yes", replace
					estadd local shock "Yes", replace
					estadd local noninter ".", replace
					estadd local mort_term "`outcome_term'", replace
					estadd local mort_ltv "`outcome_ltv'", replace
					estadd local mort_type "`outcome_mortgage_type'", replace
					estadd local sample_cov "Public", replace
					estadd local szcontrol "revt_a06"
					est sto t_basic_revt_a06 
					
				foreach t of local indep {
					
					** financial constraints regressions 
					reghdfe `outcome' shockdelta1yrZIL  c.`t'#c.shockdelta1yrZIL `t' revt_a06 `outcome_controls' `hpZIL_control'  if     `samplecut' `return_sample'    !mi(shockdelta1yrZIL), absorb(`timefe'#`spacefe' `timefe' `spacefe') vce(cluster `s')
						estadd local fixedyr "Yes" , replace
						estadd local fixedfips "Yes" , replace
						estadd local fixedyrfips "Yes" , replace
						estadd local sqfeet "No", replace
						estadd local hpZIL "`hpZIL_out'", replace
						estadd local assets "Yes", replace
						estadd local shock "Yes", replace
						estadd local noninter "Yes", replace
						estadd local mort_term "`outcome_term'", replace
						estadd local mort_ltv "`outcome_ltv'", replace
						estadd local mort_type "`outcome_mortgage_type'", replace
						estadd local sample_cov "Public", replace
						estadd local szcontrol "revt_a06"
						est sto t_`t'_revt_a06
						
				}	
			
		
		*** homes sold in second half of 06, built before 06, to homes sold in 06
		label var inv_sales_06 "Inventory to Sales_{06}"
				
			reghdfe lnsaleamt c.shockdelta1yrZIL##c.inv_sales_06 loghpZIL_f rev_all_06  if !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_IND) & !mi(shockdelta1yrZIL) &  !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
				estadd local fixedyr "Yes" , replace
				estadd local fixedfips "Yes" , replace
				estadd local fixedyrfips "Yes" , replace
				estadd local sqfeet "No", replace
				estadd local hpZIL "`hpZIL_out'", replace
				estadd local assets "Yes", replace
				estadd local shock "Yes", replace
				estadd local noninter "Yes", replace
				estadd local mort_term "`outcome_term'", replace
				estadd local mort_ltv "`outcome_ltv'", replace
				estadd local mort_type "`outcome_mortgage_type'", replace
				estadd local sample_cov "All", replace
				estadd local szcontrol "rev_all_06"
				est sto t_ivs_rev_all_06
		
		
		reghdfe `outcome' shockdelta1yrZIL lev cov pcteq equ option c.lev#c.shockdelta1yrZIL  c.cov#c.shockdelta1yrZIL ///
			c.pcteq#c.shockdelta1yrZIL c.equ#c.shockdelta1yrZIL c.option#c.shockdelta1yrZIL   ///
			 revt_a06 `outcome_controls' `hpZIL_control'  if     `samplecut' `return_sample'    !mi(shockdelta1yrZIL), absorb(`timefe'#`spacefe' `timefe' `spacefe') vce(cluster `s')
				estadd local fixedyr "Yes" , replace
				estadd local fixedfips "Yes" , replace
				estadd local fixedyrfips "Yes" , replace
				estadd local sqfeet "Yes", replace
				estadd local hpZIL "`hpZIL_out'", replace
				estadd local assets "Yes", replace
				estadd local shock "Yes", replace
				estadd local noninter "Yes", replace
				estadd local mort_term "`outcome_term'", replace
				estadd local mort_ltv "`outcome_ltv'", replace
				estadd local mort_type "`outcome_mortgage_type'", replace
				estadd local sample_cov "Public", replace
				est sto b_all_r06
		
		** TABLE 4
		esttab t_basic_revt_a06 t_lev_revt_a06 t_cov_revt_a06 t_pcteq_revt_a06 t_equ_revt_a06 t_option_revt_a06 t_ivs_rev_all_06  using   ///       
			"`file_path_in'/reg output/`fin_exploration'_TABLE4.tex",   replace se label modelwidth(6) nonotes ///
		s(fixedyr fixedfips fixedyrfips hpZIL assets shock noninter  sample_cov r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "County HP Index" "Rev." "Shock" "Non Inter. Var"  "Sample" "\$ R^2 \$")) ///
			drop(`hpZILdrop'  revt_a06 rev_all_06 lev cov option pcteq equ inv_sales_06 )  ///
			mtitles("Rev., 06" "Rev., 06" "Rev., 06" "Rev., 06" "Rev., 06" "Rev., 06" "Rev., 06" )  ///
			nonotes starlevels(* 0.10 ** 0.05 *** 0.01) 
		
		esttab t_basic_revt_a06 t_lev_revt_a06 t_cov_revt_a06 t_pcteq_revt_a06 t_equ_revt_a06 t_option_revt_a06 t_ivs_rev_all_06  using   ///       
			"`file_path_in'/reg output/`fin_exploration'_TABLE4.csv",   replace se label modelwidth(6) nonotes ///
		s(fixedyr fixedfips fixedyrfips hpZIL assets shock noninter  sample_cov r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "County HP Index" "Rev." "Shock" "Non Inter. Var"  "Sample" "\$ R^2 \$")) ///
			drop(`hpZILdrop'  revt_a06 rev_all_06 lev cov option pcteq equ inv_sales_06 )  ///
			mtitles("Rev., 06" "Rev., 06" "Rev., 06" "Rev., 06" "Rev., 06" "Rev., 06" "Rev., 06" )  ///
			nonotes starlevels(* 0.10 ** 0.05 *** 0.01) 
			
		** kitchen sink reg, for appendix
		esttab b_all_r06  using ///       
			"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'_`fin_exploration'_allcovs.csv",   replace se label modelwidth(6) nonotes ///
		s(fixedyr fixedfips fixedyrfips hpZIL assets shock noninter  sample_cov r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "County HP Index" "Rev." "Shock" "Non Inter. Var" "No. States" "Sample" "\$ R^2 \$")) ///
			drop(`hpZILdrop'   revt_a06 lev cov option pcteq equ)  ///
			mtitles("Price"  )  ///
			nonotes starlevels(* 0.10 ** 0.05 *** 0.01) 
			
		esttab b_all_r06  using ///       
			"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'_`fin_exploration'_allcovs.tex",   replace se label modelwidth(6) nonotes ///
		s(fixedyr fixedfips fixedyrfips hpZIL assets shock noninter  sample_cov r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "County HP Index" "Rev." "Shock" "Non Inter. Var" "No. States" "Sample" "\$ R^2 \$")) ///
			drop(`hpZILdrop' revt_a06 lev cov option pcteq equ)  ///
			mtitles("Price"  )  ///
			nonotes starlevels(* 0.10 ** 0.05 *** 0.01) 
		
		}
		
		** rename fin constraints vars back to original
		rename lev leverage_q06_mean 
		rename cov coverage_q06_mean 
		rename option pctoption_a06 
		rename pcteq pcteq_subsassets_a06 
		rename equ eq_unconsol_a06 
	}
		
	********************************** heterogeneity analysis: home type and HHI  ********************************** 
	* effect split by housing characteristics - size, quality, price tier AND by builder county HHI (share of homes builder sold in a county in 06)
	local clusters fips 
	if   "`regtype'"!="chg0609" & "`regplacebo'"=="" & "`fin_exploration'"=="" & "`regoutcome'"!="lnhpzilzip"  & "`regoutcome'"!="time_mkt"  & "`regoutcome'"!="time_mktGENERATE"  & "`regsizeplot'"!="sizeplot" & "`regoutcome'"!="no_sales" & "`regoutcome'"!="duration"  & "`regcontrols'"==""           & "`regcontrols'"!="shockattributes"& "`durationplot'"!="duration" &  "`regzipquality'"=="" & "`houscharhetero'" == "houshetero" {   
		foreach s of local clusters {
		
			egen sqfeet_2 = xtile(sqfeet) if !mi(shockdelta1yrZIL) & !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , nq(2)
			egen baths_2 = xtile(baths) if !mi(shockdelta1yrZIL) & !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , nq(2)
			egen price_tier_2 = xtile(saleamount) if !mi(shockdelta1yrZIL) & !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , nq(2)
			egen price_tier_22 = xtile(hpZIL_f) if !mi(shockdelta1yrZIL) & !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , nq(2)

			** quality code: =1 if quality code is good or excellent, 0 for all other values of quality
			g qual_high2 = QUALITY_=="QEX" | QUALITY=="QGO" 
			replace qual_high2 = . if QUALITY==""
			g qual_good = QUALITY=="QGO"
			replace qual_good = . if QUALITY==""
			g qual_high5 = QUALITY_=="QEX" | QUALITY=="QGO" | QUALITY=="QVV" 
			replace qual_high5 = . if QUALITY==""
			
			** look at characteristics of homes that are high quality
			ge slab = FOUNDATION=="SLB"
			replace slab = . if FOUNDATION==""
			g concrete = FOUNDATION=="CRE"
			replace concrete = . if FOUNDATION==""
			
			g vinyl = EXTERIOR=="VIN"
			replace vinyl = . if EXTER==""
			g stucco = EXTERIOR=="STU"
			replace stucco = . if EXTER==""
			g brick = EXTERIOR=="BRV" | EXTERIOR=="BRI" | EXTERIOR=="BRS"
			replace brick = . if EXTER==""
			
			** quality by shock - builders more likely to spread shock to higher quality buildings - larger, and higher quality
			reghdfe lnsaleamt ib1.sqfeet_2##c.shockdelta1yrZIL   loghpZIL_f   if   !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "Yes", replace
			estadd local sqfeet "No", replace
			estadd local baths "No", replace
			estadd local fixedprop "No", replace
			est sto s1
			
			reghdfe lnsaleamt ib1.sqfeet_2##c.shockdelta1yrZIL   loghpZIL_f baths i.PROPERTY_INDICATOR_CODE  if   !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "Yes", replace
			estadd local sqfeet "No", replace
			estadd local baths "Yes", replace
			estadd local fixedprop "Yes", replace
			est sto s2
			
			reghdfe lnsaleamt i.baths_2##c.shockdelta1yrZIL loghpZIL_f  if   !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "Yes", replace
			estadd local sqfeet "No", replace
			estadd local baths "No", replace
			estadd local fixedprop "No", replace
			est sto b1
			
			reghdfe lnsaleamt i.baths_2##c.shockdelta1yrZIL  loghpZIL_f sqfeet i.PROPERTY_IND if   !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "Yes", replace
			estadd local sqfeet "Yes", replace
			estadd local baths "No", replace
			estadd local fixedprop "Yes", replace
			est sto b2
			
			reghdfe lnsaleamt i.qual_high2##c.shockdelta1yrZIL loghpZIL_f  sqfeet  if   !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "Yes", replace
			estadd local sqfeet "Yes", replace
			estadd local baths "No", replace
			estadd local fixedprop "No", replace
			est sto q1
			
			reghdfe lnsaleamt i.qual_high2##c.shockdelta1yrZIL loghpZIL_f  sqfeet baths i.PROPERTY_IN if   !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "Yes", replace
			estadd local sqfeet "Yes", replace
			estadd local baths "Yes", replace
			estadd local fixedprop "Yes", replace
			est sto q2
		
			reghdfe lnsaleamt c.bldr_share2_06##c.shockdelta1yrZIL loghpZIL_f sqfeet if  !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_IN) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			estadd local sqfeet "Yes", replace
			estadd local baths "No", replace
			estadd local fixedprop "No", replace
			est sto hhi1
		
			reghdfe lnsaleamt c.bldr_share2_06##c.shockdelta1yrZIL loghpZIL_f sqfeet baths i.PROPERTY_IND  if  !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_IN) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			estadd local sqfeet "Yes", replace
			estadd local baths "Yes", replace
			estadd local fixedprop "Yes", replace
			est sto hhi2
			
			esttab  s1 s2 b1 b2 q1 q2 hhi2 using ///   
				"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'_hhi.csv",   replace se label ///
				s( fixedfips  hpZIL sqfeet baths fixedprop r2 N , label("`spacefeout' FE"  "`hpcontrol_out'" ///
				"Sq. Ft." "Baths" "Prop. Type FE"    "\$ R^2 \$")) substitute(\_ _) ///
					 mgroups("All Firms" , pattern(1 0 0 0 0 0 0))  varwidth(1) `finlabel' nonotes ///
					rename(1.qual_high2#c.shockdelta1yrZIL shock_highqual, 2.baths_2#c.shockdelta1yrZIL shock_highbaths, 1.sqfeet_2#c.shockdelta1yrZIL shock_lowsqfeet c.bldr_share2_06#c.shockdelta1yrZIL shock_hhi)   ///
					mtitles("Price" "Price" "Price" "Price" "Price" "Price" "Price") ///
					drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop'  `vardrop' `assetdrop' bldr_share2_06 *PROPERTY_INDICATOR_CODE* 0.qual_high2#c.shockdelta1yrZIL  1.qual_high2 2.baths_2 1.baths_2#c.shockdelta1yrZIL 2.sqfeet_2#c.shockdelta1yrZIL  2.sqfeet_2 )  ///
					coeflabels(shock_highqual "Shock X High Quality" shock_highbaths "Shock X High Baths" shock_lowsqfeet "Shock X Low Sq. Ft." shock_hhi "Shock X HHI")  starlevels(* 0.10 ** 0.05 *** 0.01) 
			
			esttab  s1 s2 b1 b2 q1 q2 hhi1 using ///   
				"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'_hhi.tex",   replace se label ///
				s( fixedfips  hpZIL sqfeet baths fixedprop r2 N , label("`spacefeout' FE"  "`hpcontrol_out'" ///
				"Sq. Ft." "Baths" "Prop. Type FE"    "\$ R^2 \$")) substitute(\_ _) ///
					 mgroups("All Firms" , pattern(1 0 0 0 0 0 0))  varwidth(1) `finlabel' nonotes ///
					rename(1.qual_high2#c.shockdelta1yrZIL shock_highqual, 2.baths_2#c.shockdelta1yrZIL shock_highbaths, 1.sqfeet_2#c.shockdelta1yrZIL shock_lowsqfeet c.bldr_share2_06#c.shockdelta1yrZIL shock_hhi)   ///
					mtitles("Price" "Price" "Price" "Price" "Price" "Price" "Price") ///
					drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop'  `vardrop' `assetdrop' bldr_share2_06 *PROPERTY_INDICATOR_CODE* 0.qual_high2#c.shockdelta1yrZIL  1.qual_high2 2.baths_2 1.baths_2#c.shockdelta1yrZIL 2.sqfeet_2#c.shockdelta1yrZIL  2.sqfeet_2 )  ///
					coeflabels(shock_highqual "Shock X High Quality" shock_highbaths "Shock X High Baths" shock_lowsqfeet "Shock X Low Sq. Ft." shock_hhi "Shock X HHI") starlevels(* 0.10 ** 0.05 *** 0.01) 
			
			eststo allh1 : estpost summarize bldr_share2_06 saleamount if !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), d
			
			preserve
			collapse  bldr_share2_06 saleamount if !mi(shockdelta1yrZIL) & !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_IND), by(seller year)
			eststo allh2:  estpost summarize bldr_share2_06 saleamount, d
			restore	
		
			esttab allh1 allh2  using "/`file_path_in'/reg output/hhi_summ_stats_`regtype'`outcomeyr'`reglevel'twocolumns.csv", cells("mean(pattern(1 1 0) fmt(4)) sd(pattern(1 1 0) fmt(4)) p25(pattern(1 1 0) fmt(4)) p50(pattern(1 1 0) fmt(4)) p75(pattern(1 1 0) fmt(4)) ") label nonum replace substitute(\_ _)
		
			}
		}
		
	******************************************************************************************************************************************************************************************************
	** do financier home builders have lower price cut?
	
	else if "`regoutcome'"=="hbloan" & "`regtype'"!="chg0609" & "`regplacebo'"=="" & "`fin_exploration'"=="" &  "`regoutcome'"!="lnhpzilzip" & "`regoutcome'"!="time_mkt"  & "`regoutcome'"!="time_mktGENERATE"  &  "`regsizeplot'"!="sizeplot" & "`regoutcome'"!="no_sales" & "`regoutcome'"!="duration" & "`regcontrols'"!="allcontrols" & "`regcontrols'"!="shockattributes" & "`durationplot'"!="duration" &  "`regzipquality'" == ""  & "`houscharhetero'" == "" {   
	
		** builder mortgages: show the main effect does not change with captive financing
		* 1. interact main reg with financier dummy (with and without housing controls)
		reghdfe lnsaleamt c.shockdelta1yrZIL##i.financier loghpZIL_f i.mortgage_type mortgage_term if !mi(mortgage_term) & !mi(mortgage_type) & !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
		estadd local fixedfips "Yes" , replace
		estadd local hpZIL "Yes", replace
		estadd local sqfeet "No", replace
		estadd local baths "No", replace
		estadd local fixedprop "No", replace
		estadd local mort_term "Yes", replace
		estadd local mort_type "Yes", replace
		estadd local ttest "NA", replace
		est sto hb1
		
		reghdfe lnsaleamt c.shockdelta1yrZIL##i.financier loghpZIL_f sqfeet i.mortgage_type mortgage_term if !mi(mortgage_term) & !mi(mortgage_type) & !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
		estadd local fixedfips "Yes" , replace
		estadd local hpZIL "Yes", replace
		estadd local sqfeet "Yes", replace
		estadd local baths "No", replace
		estadd local fixedprop "No", replace
		estadd local mort_term "Yes", replace
		estadd local mort_type "Yes", replace
		estadd local ttest "NA", replace
		est sto hb2
		
		reghdfe lnsaleamt c.shockdelta1yrZIL##i.financier loghpZIL_f sqfeet baths i.PROPERTY_INDICATOR_CODE i.mortgage_type mortgage_term if !mi(mortgage_term) & !mi(mortgage_type) & !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
		estadd local fixedfips "Yes" , replace
		estadd local hpZIL "Yes", replace
		estadd local sqfeet "Yes", replace
		estadd local baths "Yes", replace
		estadd local fixedprop "Yes", replace
		estadd local mort_term "Yes", replace
		estadd local mort_type "Yes", replace
		estadd local ttest "NA", replace
		est sto hb3
		
		* 2. to test whether the coefficients on the shock when I split the sample by financier or not, are sign. different from 0, estimate reg where you interact
		* each term with the financier dummy (equivalent to splitting the sample), and then do ttest that the coefficient on the financierXshock variable is diff. from 0.
		
		reghdfe lnsaleamt c.shockdelta1yrZIL##i.financier  c.sqfeet##i.financier  c.loghpZIL_f##i.financier i.mortgage_type##i.financier c.mortgage_term##i.financier if !mi(mortgage_term) & !mi(mortgage_type) & !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip##i.financier) vce(cluster fips)
		test 0.financier#c.shockdelta1yrZIL 
		est sto hb5
		local t = _b[0.financier#c.shockdelta1yrZIL]/_se[0.financier#c.shockdelta1yrZIL]
		local p_wcontrol1 =2*ttail(e(df_r),abs(`t'))
		di "`p_wcontrol1'"
		est save "`file_path_in'/reg output/ttest_financier_w_control1.ster", replace	
		
		reghdfe lnsaleamt c.shockdelta1yrZIL##i.financier c.loghpZIL_f##i.financier c.sqfeet##i.financier c.baths##i.financier i.PROPERTY_INDICATOR_CODE##i.financier i.mortgage_type##i.financier c.mortgage_term##i.financier if !mi(mortgage_term) & !mi(mortgage_type) & !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip##i.financier) vce(cluster fips)
		test 0.financier#c.shockdelta1yrZIL 
		est sto hb4
		local t = _b[0.financier#c.shockdelta1yrZIL]/_se[0.financier#c.shockdelta1yrZIL]
		local p_wcontrol2 =2*ttail(e(df_r),abs(`t'))
		di "`p_wcontrol2'"
		est save "`file_path_in'/reg output/ttest_financier_wcontrol2.ster", replace

		* 3. split main reg sample by financier dummy (with and without housing controls)
		reghdfe lnsaleamt shockdelta1yrZIL loghpZIL_f sqfeet  i.mortgage_type mortgage_term if financier==1 & !mi(mortgage_term) & !mi(mortgage_type) & !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
		estadd local fixedfips "Yes" , replace
		estadd local hpZIL "Yes", replace
		estadd local sqfeet "Yes", replace
		estadd local baths "No", replace
		estadd local fixedprop "No", replace
		estadd local mort_term "Yes", replace
		estadd local mort_type "Yes", replace
		estadd local ttest "`=round(`p_wcontrol1',0.0001)'", replace	
		est sto hb6
		
		reghdfe lnsaleamt shockdelta1yrZIL loghpZIL_f sqfeet  i.mortgage_type mortgage_term if financier==0 & !mi(mortgage_term) & !mi(mortgage_type) & !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
		estadd local fixedfips "Yes" , replace
		estadd local hpZIL "Yes", replace
		estadd local sqfeet "Yes", replace
		estadd local baths "No", replace
		estadd local fixedprop "No", replace
		estadd local mort_term "Yes", replace
		estadd local mort_type "Yes", replace
		estadd local ttest "`=round(`p_wcontrol1',0.0001)'", replace	
		est sto hb7
		
		reghdfe lnsaleamt shockdelta1yrZIL loghpZIL_f sqfeet baths i.PROPERTY_INDICATOR_CODE i.mortgage_type mortgage_term if financier==1 & !mi(mortgage_term) & !mi(mortgage_type) & !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
		estadd local fixedfips "Yes" , replace
		estadd local hpZIL "Yes", replace
		estadd local sqfeet "Yes", replace
		estadd local baths "Yes", replace
		estadd local fixedprop "Yes", replace
		estadd local mort_term "Yes", replace
		estadd local mort_type "Yes", replace
		estadd local ttest "`=round(`p_wcontrol2',0.0001)'", replace	
		est sto hb8
		
		reghdfe lnsaleamt shockdelta1yrZIL loghpZIL_f sqfeet baths i.PROPERTY_INDICATOR_CODE i.mortgage_type mortgage_term if financier==0 & !mi(mortgage_term) & !mi(mortgage_type) & !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), absorb(i.year##i.zip) vce(cluster fips)
		estadd local fixedfips "Yes" , replace
		estadd local hpZIL "Yes", replace
		estadd local sqfeet "Yes", replace
		estadd local baths "Yes", replace
		estadd local fixedprop "Yes", replace
		estadd local mort_term "Yes", replace
		estadd local mort_type "Yes", replace
		estadd local ttest "`=round(`p_wcontrol2',0.0001)'", replace	
		est sto hb9
		
		esttab   hb2 hb3 hb6 hb7 hb8 hb9 using ///   
			"`file_path_in'/reg output/hbloan_robustness.csv",   replace se label ///
			s( fixedfips  hpZIL sqfeet baths fixedprop mort_term mort_type ttest r2 N , label("Zip FE"  "County HP" ///
			"Sq. Ft." "Baths" "Prop. Type FE" "Mort. Term" "Mort. Type" "P-value"  "\$ R^2 \$")) substitute(\_ _)  mgroups("All Firms" "Captive" "Non-Captive" "Captive" "Non-Captive", pattern(1  0 1 1 1 1 ))  ///
				mtitles("Price" "Price" "Price"  "Price"  "Price" "Price" )  ///
				drop( 0.financier#c.shockdelta1yrZIL loghpZIL_fips sqfeet baths *PROPERTY_INDICATOR_CODE   mortgage_term *mortgage_type* )  varwidth(1) nonotes starlevels(* 0.10 ** 0.05 *** 0.01) 
		
		esttab   hb2 hb3 hb6 hb7 hb8 hb9 using ///   
			"`file_path_in'/reg output/hbloan_robustness.tex",   replace se label ///
			s( fixedfips  hpZIL sqfeet baths fixedprop mort_term mort_type ttest r2 N , label("Zip FE"  "County HP" ///
			"Sq. Ft." "Baths" "Prop. Type FE" "Mort. Term" "Mort. Type" "P-value"  "\$ R^2 \$")) substitute(\_ _)  mgroups("All Firms" "Captive" "Non-Captive" "Captive" "Non-Captive", pattern(1  0 1 1 1 1 ))  ///
				mtitles("Price" "Price" "Price"  "Price"  "Price" "Price" )  ///
				drop( 0.financier#c.shockdelta1yrZIL loghpZIL_fips sqfeet baths *PROPERTY_INDICATOR_CODE   mortgage_term *mortgage_type* )  varwidth(1) nonotes starlevels(* 0.10 ** 0.05 *** 0.01) 
	
	}

******************************* elasticity analysis ******************************* 	
if  "`regsizeplot'"=="sizeplot" &  "`regplacebo'"=="" & "`fin_exploration'"=="" & "`regoutcome'"!="lnhpzilzip"  &  "`regoutcome'"!="duration" & "`regoutcome'"!="notsoldbuilt" & "`regcontrols'"!="allcontrols" & "`durationplot'"!="duration" &  "`regzipquality'" == ""  & "`houscharhetero'" == ""   {
		
		
		merge m:1 fips using "`file_path_in'//county transactions/elasticity_tomerge.dta", keep(master match)
		
		save "`file_path_in'/elasticity_analysis.dta", replace
		
		use "/`file_path_in'/elasticity_analysis.dta", clear
		gen sale = !mi(shockdelta1yrZIL) & !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE)
		bysort fips: gen nhomes_fips = sum(sale)
		bysort fips: replace nhomes_fips = nhomes_fips[_N]
		g lnnhomes_fips = log(nhomes_fips)
		g lnelasticity_N = log(elasticity_N)
		
		rename elasticity_b slope
		g elasticity = (-1)*(slope*elasticity_d)  // multiply the slope by avg_d, the average prob of selling within 91 days in that county, to get elasticity for county
		summ elasticity, d
		local min `r(p5)'
		local max `r(p95)'
		replace elasticity = . if elasticity<`min' | elasticity>`max'
		replace slope = -1*slope
		summ slope, d
		local min `r(p5)'
		local max `r(p95)'
		replace slope = . if   slope>`max'  | slope<`min'
		local bins  4
		cap drop elast_b_bin
		egen elast_b_bin = xtile(slope) if !mi(sqfeet)  & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) &  !mi(shockdelta1yrZIL) & nhomes_fips>20 , nq(`bins')
		forval c=1/`bins' { 
			reghdfe lnsaleamt shockdelta1yrZIL sqfeet baths i.PROPERTY_INDICATOR_CODE loghpZIL_fips if elast_b_bin==`c', absorb(i.year##i.zip) vce(cluster fips)
			eststo c`c'
		}
		coefplot (c1, label("1")) || (c2, label("2")) || (c3, label("3")) || (c4, label("4"))        , keep(shockdelta1yrZIL)  yline(0) vertical bycoefs byopts(yrescale) ytitle("Coefficient on Builder Shock") xtitle("Quantiles of County Time to Sale-Price Sensitivity") ///
			graphregion(color(white))  
		graph export "`file_path_in'/reg output//elasticity_quantile`regoutcome'`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'.png", replace
		graph export "`file_path_in'/reg output//elasticity_quantile`regoutcome'`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'.eps", replace	
					
	}

*******************************  differences regs ******************************* 	
else if "`regtype'"=="chg0609" {

	use "`file_path_in'/financialsq.dta", clear
		ds seller year conm tic cusip, not
		collapse `r(varlist)', by(seller year)
	save "`file_path_in'/financialsa.dta", replace
	
	use "`file_path_in'/builder county/rawBUILDERmergedHPshockAGGR_`reglevel'.dta", clear 
	merge m:1 seller year using "`file_path_in'/financialsa.dta", keep(master match)
	gen public_firm = _merge==3 
	local noshock00609 "if shockdelta0609ZIL_`reglevel'!=0"
	local noshock00205 "if shockdelta0205ZIL_`reglevel'!=0"

	* diff regs
	reg chg0609 shockdelta0609ZIL_`reglevel' delta0609ZIL_`reglevel' `noshock00609' , vce(cluster FIPS_CODE)
	est sto c1
	reg chg0205 shockdelta0205ZIL_`reglevel' delta0205ZIL_`reglevel'  `noshock00205' , vce(cluster FIPS_CODE)
	est sto c2

	esttab  c1 c2   using "`file_path_in'/reg output/fips`regtype'`regplacebo'`outcomeyr'`regcontrols'`reglevel'`inclpublic'`regzipquality'`houscharhetero'.tex",  replace se label ///
	  nonotes s(r2 N , )  keep( shock*  delta*) mgroups("All Firms" , pattern(1 0  )) ///
	  mtitles("\$ \Delta Price\$" "\$ \Delta Price\$"  )  starlevels(* 0.10 ** 0.05 *** 0.01) 
	esttab  c1 c2   using "`file_path_in'/reg output/fips`regtype'`regplacebo'`outcomeyr'`regcontrols'`reglevel'`inclpublic'`regzipquality'`houscharhetero'.csv",  replace se label ///
	  nonotes s(r2 N , )  keep( shock*  delta*) mgroups("All Firms" , pattern(1 0  )) ///
	  mtitles("\$ \Delta Price\$" "\$ \Delta Price\$"  )  starlevels(* 0.10 ** 0.05 *** 0.01) 
}

******************************* listing times ******************************* 
if "`regtype'"=="main09" &  "`regoutcome'"=="time_mkt" &  ///
	 "`reglevel'"=="fips"   & "`regplacebo'"=="" & "`fin_exploration'"==""  & "`regsizeplot'"!="sizeplot" & "`regoutcome'"!="no_sales" & "`regoutcome'"!="duration" & "`regcontrols'"!="allcontrols" & "`durationplot'"!="duration" &  "`regzipquality'" == ""  & "`houscharhetero'" == ""   {
			
		do "`file_path_code'/merge-MLS-deeds.do"
		
		local file _path_in "/tochange/"
		use "`file_path_in'/alllistings_for_regs2.dta", clear
		
		** create summ stats of listings
		gen sf = PROPERTY_INDICATOR_CODE==10
		gen condo = PROPERTY_INDICATOR_CODE==11
		gen duplex = PROPERTY_INDICATOR_CODE==12
		label var saleamount "Sale Price"
		label var time_mkt "Listing Time (Days)"
		label var condo "Condo"
		label var sf "Single Family"
		label var duplex "Duplex"
		label var sqfeet "Sq. Ft."
		label var baths "No. Baths"
		
		local file _path_in "/tochange/"
		eststo all: quietly estpost summarize time_mkt saleamount  sqfeet baths condo duplex sf if !mi(time_mkt) & !mi(saleamount) & !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), d
		eststo public: quietly estpost summarize time_mkt saleamount sqfeet baths condo duplex sf  if coverage_q06_mean!=. & !mi(time_mkt) & !mi(saleamount) & !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) & !mi(shockdelta1yrZIL), d
		esttab all public using "`file_path_in'/reg output/list_summ_stats_`regtype'`outcomeyr'`reglevel'twocolumns.csv", ///
			cells("mean(pattern(1 1 0) fmt(2)) sd(pattern(1 1 0)) p50(pattern(1 1 0) fmt(2)) ") label mtitles("All Firms" "Public Firms") nonum replace substitute(\_ _) starlevels(* 0.10 ** 0.05 *** 0.01) 
		esttab all public using "`file_path_in'/reg output/list_summ_stats_`regtype'`outcomeyr'`reglevel'twocolumns.csv", ///
			cells("mean(pattern(1 1 0) fmt(2)) sd(pattern(1 1 0)) p50(pattern(1 1 0) fmt(2)) ") label mtitles("All Firms" "Public Firms") nonum replace substitute(\_ _) starlevels(* 0.10 ** 0.05 *** 0.01) 

		*** duration analysis
		gen sold=1
		keep if shockdelta1yrZIL!=.
		sum shockdelta1yrZIL if shockdelta1yrZIL!=., d
		local median_shock = `r(p50)'
		gen below_median_shock = shockdelta1yrZIL<=`median_shock'
		
		stset closedate, failure(sold) origin(listdate) enter(listdate)
			
		streg i.below_median_shock sqfeet baths i.PROPERTY_INDICATOR_CODE loghpZIL_`reglevel' i.zipcode if !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) , di(weibull) nolog nohr vce(cluster fips)
		stcurve, survival at1(below_median_shock=1) at2(below_median_shock=0) graphregion(color(white)) title("") legend(lab(1 "Below Median Shock") lab(2 "Above Median Shock")) 
		graph export "`file_path_in'/reg output//survival_listing`regoutcome'`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`reglevel'.png", replace
		graph export "`file_path_in'/reg output//survival_listing`regoutcome'`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`reglevel'.eps", replace
		
	** Table 5
	if "`regtype'"!="chg0609" & "`regplacebo'"=="" & "`fin_exploration'"=="" & "`regoutcome'"=="time_mkt" &  "`regsizeplot'"!="sizeplot" & "`regoutcome'"!="no_sales" & "`regoutcome'"!="duration" & "`regcontrols'"!="allcontrols" & "`durationplot'"!="duration" &  "`regzipquality'" == ""  & "`houscharhetero'" == ""  {   
		foreach s of local clusters {
		 
			qui  reghdfe time_mkt `shock' `outcome_controls' `hpZIL_control'  if    `samplecut' `return_sample'   !mi(lnsaleamt) &  !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			estadd local sqfeet "No", replace
			estadd local baths "No", replace
			estadd local fixedprop "No", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "`finout'", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c1

			qui  reghdfe time_mkt `shock' `outcome_controls' `hpZIL_control'  `sqfeet'  if    `samplecut' `return_sample'   !mi(lnsaleamt) &  !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			if "`sqfeet'"=="sqfeet" {
				estadd local sqfeet "Yes", replace
			}
			else estadd local sqfeet "No", replace
			estadd local baths "No", replace
			estadd local fixedprop "No", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "`finout'", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c2

			qui  reghdfe time_mkt `shock' `outcome_controls' `hpZIL_control'   `sqfeet' `baths'  if     `samplecut' `return_sample'    !mi(lnsaleamt) & !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			if "`sqfeet'"=="sqfeet" {
				estadd local sqfeet "Yes", replace
			}
			else estadd local sqfeet "No", replace
			if "`baths'"=="baths" {
				estadd local baths "Yes", replace
			}
			else estadd local baths "No", replace
			estadd local fixedprop "No", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "`finout'", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c3

			qui  reghdfe time_mkt `shock' `outcome_controls' `hpZIL_control'   `sqfeet' `baths' `prop_code'  if     `samplecut' `return_sample'    !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			if "`sqfeet'"=="sqfeet" {
				estadd local sqfeet "Yes", replace
			}
			else estadd local sqfeet "No", replace
			if "`baths'"=="baths" {
				estadd local baths "Yes", replace
			}
			else estadd local baths "No", replace
			if "`prop_code'"=="i.PROPERTY_INDICATOR_CODE" {
				estadd local fixedprop "Yes", replace
			}
			else estadd local fixedprop "No", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "`finout'", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c4

			qui  reghdfe lnsaleamt `shock' `outcome_controls' `hpZIL_control'   `sqfeet' `baths' `prop_code'  if     `samplecut' `return_sample'   !mi(time_mkt) &  !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			if "`sqfeet'"=="sqfeet" {
				estadd local sqfeet "Yes", replace
			}
			else estadd local sqfeet "No", replace
			if "`baths'"=="baths" {
				estadd local baths "Yes", replace
			}
			else estadd local baths "No", replace
			if "`prop_code'"=="i.PROPERTY_INDICATOR_CODE" {
				estadd local fixedprop "Yes", replace
			}
			else estadd local fixedprop "No", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "`finout'", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c5
			
			**  public only sample
			qui  reghdfe time_mkt `shock' `outcome_controls' `hpZIL_control' sqfeet baths i.PROPERTY_INDICATOR_CODE assets_a06   if   coverage_q06_mean!=. &  !mi(lnsaleamt) &  `samplecut' `return_sample'    !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			estadd local sqfeet "Yes", replace
			estadd local baths "Yes", replace
			estadd local fixedprop "Yes", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "Yes", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c6	
			
			**  public only sample
			qui  reghdfe lnsaleamt `shock' `outcome_controls' `hpZIL_control' sqfeet baths i.PROPERTY_INDICATOR_CODE assets_a06   if   coverage_q06_mean!=. &  !mi(time_mkt) &   `samplecut' `return_sample'    !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) , absorb(`timefe'#`spacefe' `timefe' `spacefe'  `qpair_fe') vce(cluster `s')
			estadd local fixedyr "Yes" , replace
			estadd local fixedfips "Yes" , replace
			estadd local fixedyrfips "Yes" , replace
			estadd local hpZIL "`hpZIL_out'", replace
			estadd local sqfeet "Yes", replace
			estadd local baths "Yes", replace
			estadd local fixedprop "Yes", replace
			estadd local qpairfe "`qpairfeout'", replace
			estadd local lnassets "Yes", replace
			estadd local mort_term "`outcome_term'", replace
			estadd local mort_ltv "`outcome_ltv'", replace
			estadd local mort_type "`outcome_mortgage_type'", replace
			est sto c7	
			
			esttab  c1 c2 c3 c4 c5 c6 c7 using ///   
				"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'.tex",   replace se label ///
				s(fixedyr fixedfips fixedyrfips hpZIL sqfeet baths fixedprop  qpairfe  mort_term mort_ltv mort_type lnassets r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "`hpcontrol_out'" ///
				"Sq. Ft." "Baths" "Prop. Type FE"  "Builder FE" "2nd Sale Pair FE" "Mort. Term" "LTV" "Mort. Type" "Ln Assets" "\$ R^2 \$")) substitute(\_ _) mtitles("Time" "Time" "Time" "Time" "Price" "Time" "Price")  mgroups("All Firms" "Public Only", pattern(1 0 0 0 0 1 0))  ///
					drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop' `vardrop' assets_a06 )  varwidth(1) `finlabel' nonotes   starlevels(* 0.10 ** 0.05 *** 0.01) 
					
			esttab  c1 c2 c3 c4 c5 c6 c7 using ///   
				"`file_path_in'/reg output/`regoutcome'_clust`s'`regtype'`reghetero'`regtimefe'`regplacebo'`outcomeyr'`regcontrols'`hpZIL_control'`reglevel'`inclpublic'`regzipquality'`houscharhetero'.csv",   replace se label ///
				s(fixedyr fixedfips fixedyrfips hpZIL sqfeet baths fixedprop  qpairfe  mort_term mort_ltv mort_type lnassets r2 N , label("`timefeout' FE" "`spacefeout' FE" "`timefeout' x `spacefeout'" "`hpcontrol_out'" ///
				"Sq. Ft." "Baths" "Prop. Type FE"  "Builder FE" "2nd Sale Pair FE" "Mort. Term" "LTV" "Mort. Type" "Ln Assets" "\$ R^2 \$")) substitute(\_ _) mtitles("Time" "Time" "Time" "Time" "Price" "Time" "Price")  mgroups("All Firms" "Public Only", pattern(1 0 0 0 0 1 0))  ///
					drop(`hpZILdrop' `sqfeet_drop' `baths_drop' `prop_drop' `vardrop' assets_a06 )  varwidth(1) `finlabel' nonotes   starlevels(* 0.10 ** 0.05 *** 0.01) 
					
				}
		}
	}
	
cap erase "`file_path_in'/forplaceboreg.dta"

timer off 1
timer list 1
